<?php
//+----------------------------------------------------------------------
//|版权所有：山西创泰网络科技有限公司
//+----------------------------------------------------------------------
//|Author: dtt Date: 2021/8/22 Time: 17:06
//+----------------------------------------------------------------------
namespace services;

use Qcloud\Sms\SmsSingleSender;

class QCloudSmsService
{
    // 短信应用 SDK AppID
    protected $appId = 1400555435; // SDK AppID 以1400开头
    // 短信应用 SDK AppKey
    protected $appKey = "8b0027aee4c1b01b0c6d1b095af64036";
    // 短信模板 ID，需要在短信控制台中申请
    const TEMP_ID = [
        'login'         => 1059799,
        'reser_success' => 1081351,
        'reser_fail'    => 1081355,
        'change_date'   => 1102024,
        'user_cancel'   => 1095196,
        'return_visit'  => 1102122,
        'stay_hospital'  => 1108095,
    ];

    // 签名
    protected $smsSign = "萌芽公益";

    private static $instance = null;

    static $mobile;

    public static function create(string $mobile)
    {
        self::$mobile = $mobile;
        if (self::$instance instanceof self) {
            return self::$instance;
        }
        return new self();
    }

    private function sendSms(int $tmpId, ...$arg)
    {
        try {
            if (!self::$mobile) {
                exception('请填写发送短信手机号！');
            }
            $sender = new SmsSingleSender($this->appId, $this->appKey);
            $sender->sendWithParam("86", self::$mobile, $tmpId, $arg ?? [], $this->smsSign);
        } catch (\Exception $e) {
            exit($e->getMessage());
        }
    }

    public function sendLoginSms(int $code)
    {
        $this->sendSms(self::TEMP_ID['login'], $code);
    }

    public function sendReserSucSms(string $date)
    {
        $this->sendSms(self::TEMP_ID['reser_success'], $date);
    }

    public function sendReserFailSms(string $date)
    {
        $this->sendSms(self::TEMP_ID['reser_fail'], $date);
    }

    public function sendChangeDateSms(string $oldDate, string $newData)
    {
        $this->sendSms(self::TEMP_ID['change_date'], $oldDate, $newData);
    }

    public function sendUserCancelSms(string $date)
    {
        $this->sendSms(self::TEMP_ID['user_cancel'], $date);
    }

    public function sendReturnVisitSms()
    {
        $this->sendSms(self::TEMP_ID['return_visit']);
    }

    public function sendStayHospitalSms(string $name, string $date)
    {
        $this->sendSms(self::TEMP_ID['stay_hospital'], $name, $date);
    }
}